package com.winter.base;

import lombok.Data;

import java.io.Serializable;

/**
 * @author Jinhao
 * @create 2023/02/27 13:46
 * @description 用于统一返回
 */
@Data
public class WinterResponse<T> implements Serializable {

    /**
     * 状态码
     */
    private String statusCode = "000000";

    /**
     * 状态描述
     */
    private String statusText = "success";

    /**
     * 数据
     */
    private T data;

    public WinterResponse(String statusCode, String statusText) {
        this.statusCode = statusCode;
        this.statusText = statusText;
        this.data = null;
    }

    /*==============================================基于BusinessCode的构造方法===========================================*/

    public WinterResponse(BusinessCode businessCode) {
        this.statusCode = businessCode.getCode();
        this.statusText = businessCode.getDesc();
        this.data = null;
    }

    public WinterResponse(BusinessCode businessCode, T data) {
        this.statusCode = businessCode.getCode();
        this.statusText = businessCode.getDesc();
        this.data = data;
    }

    /*==============================================success / error====================================================*/

    public static WinterResponse<Object> success() {
        return new WinterResponse<>(BusinessCode.SUCCESS);
    }

    public static WinterResponse<Object> success(Object data) {
        return new WinterResponse<>(BusinessCode.SUCCESS, data);
    }

    public static WinterResponse<Object> error() {
        return new WinterResponse<>(BusinessCode.FATAL);
    }

    public static WinterResponse<Object> error(String errorMsg) {
        return new WinterResponse<>(BusinessCode.FATAL.getCode(), errorMsg);
    }

    public static WinterResponse<Object> error(BusinessCode businessCode, String errorMsg) {
        return new WinterResponse<>(businessCode.getCode(), errorMsg);
    }

    /**
     * 成功状态判断
     *
     * @return boolean
     */
    public boolean successful() {
        return this.statusCode.equals(BusinessCode.SUCCESS.getCode());
    }
}
